package transfer

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	pb "gitee.com/liuxuezhan/ar-platform/rpc-client/safeBoxClient/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListTransferRecordLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取保险箱转账收益记录
func NewListTransferRecordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListTransferRecordLogic {
	return &ListTransferRecordLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListTransferRecordLogic) ListTransferRecord(req *types.ListTransferRecordRequest) (*types.ListTransferRecordResponse, error) {
	// 请求数据
	in := &pb.ListTransferRecordRequest{
		Page:         req.Page,
		PageSize:     req.PageSize,
		Uid:          req.Uid,
		TransferType: req.TransferType,
		StartTime:    req.StartTime,
		EndTime:      req.EndTime,
	}
	// RPC请求
	out, err := l.svcCtx.SafeBoxRpcService.Transfer.ListTransferRecord(l.ctx, in)
	if err != nil {
		return nil, err
	}
	// 响应数据
	resp := &types.ListTransferRecordResponse{
		Meta: types.ListTransferRecord_Meta{
			Total:    out.Meta.Total,
			Page:     out.Meta.Page,
			PageSize: out.Meta.PageSize,
		},
		Stats: types.ListTransferRecord_Stats{
			Uid:         out.Stats.Uid,
			Balance:     out.Stats.Balance,
			IncomeTotal: out.Stats.IncomeTotal,
			LastInTime:  out.Stats.LastInTime,
			LastOutTime: out.Stats.LastOutTime,
			Rate:        out.Stats.Rate,
			RateVip:     out.Stats.RateVip,
			IncomeTodo:  out.Stats.IncomeTodo,
		},
	}
	// 数据映射
	n := len(out.Data)
	resp.Data = make([]types.ListTransferRecord_Data, 0, n)
	for i := 0; i < n; i++ {
		data := types.ListTransferRecord_Data{
			Sn:              out.Data[i].Sn,
			Uid:             out.Data[i].Uid,
			TransferType:    out.Data[i].TransferType,
			TransferInTime:  out.Data[i].TransferInTime,
			TransferOutTime: out.Data[i].TransferOutTime,
			Amount:          out.Data[i].Amount,
			Rate:            out.Data[i].Rate,
			RateVip:         out.Data[i].RateVip,
			Income:          out.Data[i].Income,
			CreatedAt:       out.Data[i].CreatedAt,
		}
		resp.Data = append(resp.Data, data)
	}

	return resp, nil
}
